<?php

use Fisharebest\Webtrees\Auth;
use Fisharebest\Webtrees\Http\RequestHandlers\ControlPanel;
use Fisharebest\Webtrees\Http\RequestHandlers\ModulesAllPage;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Module\ModuleConfigInterface;
use Fisharebest\Webtrees\Module\ModuleCustomInterface;
use Fisharebest\Webtrees\Module\ModuleExternalUrlInterface;
use Fisharebest\Webtrees\Module\ModuleInterface;
use Fisharebest\Webtrees\Tree;
use Fisharebest\Webtrees\View;
use Illuminate\Support\Collection;

/**
 * @var array<string,array<string,string>> $access_summary
 * @var string                             $description
 * @var string                             $interface
 * @var Collection<ModuleInterface>        $modules
 * @var string                             $title
 * @var Collection<Tree>                   $trees
 * @var bool                               $uses_access
 * @var bool                               $uses_sorting
 */

?>

<?= view('components/breadcrumbs', ['links' => [route(ControlPanel::class) => I18N::translate('Control panel'), route(ModulesAllPage::class) => I18N::translate('All modules'), $title]]) ?>

<h1><?= $title ?></h1>

<p><?= $description ?></p>

<form method="post">
    <?= csrf_field() ?>
    <table class="table table-bordered wt-table-menu">
        <thead>
            <tr>
                <th><?= I18N::translate('Module') ?></th>
                <th class="text-center"><?= I18N::translate('Enabled') ?></th>
                <?php if ($uses_access) : ?>
                    <th colspan="2" class="text-center"><?= I18N::translate('Access level') ?></th>
                <?php endif ?>
                <?php if ($uses_sorting) : ?>
                    <th class="text-center"><?= I18N::translate('Move up') ?></th>
                    <th class="text-center"><?= I18N::translate('Move down') ?></th>
                <?php endif ?>
            </tr>
        </thead>

        <tbody>
            <?php foreach ($modules as $module_name => $module) : ?>
                <tr>
                    <th scope="col">
                        <input type="hidden" name="order[]" value="<?= e($module->name()) ?>">
                        <span title="<?= e($module->description()) ?>">
                            <?= $module->title() ?>
                        </span>
                        <?php if ($module instanceof ModuleConfigInterface && $module->isEnabled()) : ?>
                            <a href="<?= e($module->getConfigLink()) ?>" title="<?= I18N::translate('Preferences') ?>">
                                <?= view('icons/preferences') ?>
                                <span class="sr-only">
                                    <?= I18N::translate('Preferences') ?>
                                </span>
                            </a>
                        <?php endif ?>
                        <?php if ($module instanceof ModuleCustomInterface) : ?>
                            <?= view('admin/custom-module-info', ['module' => $module]) ?>
                        <?php endif ?>
                        <?php if ($module instanceof ModuleExternalUrlInterface) : ?>
                            <?= view('admin/external-module-info', ['module' => $module]) ?>
                        <?php endif ?>
                    </th>

                    <td class="text-center">
                        <label class="d-block">
                            <input type="checkbox" name="status-<?= e($module->name()) ?>"
                                   id="status-<?= e($module->name()) ?>" <?= $module->isEnabled() ? 'checked' : '' ?>>
                            <span class="sr-only">
                                <?= I18N::translate('Enabled') ?>
                            </span>
                        </label>
                    </td>

                    <?php if ($uses_access) : ?>
                        <td>
                            <ul class="list-unstyled">
                                <?php foreach ($access_summary[$module->name()] as $level) : ?>
                                    <li><?= $level ?></li>
                                <?php endforeach ?>
                            </ul>

                            <div class="modal fade" id="access-level-<?= $module->name() ?>" tabindex="-1"
                                 role="dialog">
                                <div class="modal-dialog" role="document">
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <h2 class="modal-title">
                                                <?= e($module->title()) ?> – <?= I18N::translate('Access level') ?>
                                            </h2>
                                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                                <span aria-hidden="true">&times;</span>
                                            </button>
                                        </div>
                                        <div class="modal-body">
                                            <table class="table table-sm">
                                                <tbody>
                                                    <?php foreach ($trees as $tree) : ?>
                                                        <tr>
                                                            <td>
                                                                <?= e($tree->title()) ?>
                                                            </td>
                                                            <td>
                                                                <?= view('components/select', ['name' => 'access-' . $module->name() . '-' . $tree->id(), 'selected' => $module->accessLevel($tree, $interface), 'options' => Auth::accessLevelNames()]) ?>
                                                        </tr>
                                                    <?php endforeach ?>
                                                </tbody>
                                            </table>
                                        </div>
                                        <div class="modal-footer">
                                            <button type="button" class="btn btn-primary" data-dismiss="modal">Close
                                            </button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </td>

                        <td>
                            <button type="button" class="btn btn-link" data-toggle="modal" data-backdrop="static"
                                    data-target="#access-level-<?= $module->name() ?>">
                                <?= view('icons/edit') ?>
                                <span class="sr-only"><?= I18N::translate('edit') ?></span>
                            </button>
                        </td>
                    <?php endif ?>

                    <?php if ($uses_sorting) : ?>
                        <td class="move up text-center">
                            <a href="#" title="<?= I18N::translate('Move up') ?>">
                                <?= view('icons/arrow-up') ?>
                            </a>
                        </td>

                        <td class="move down text-center">
                            <a href="#" title="<?= I18N::translate('Move down') ?>">
                                <?= view('icons/arrow-down') ?>
                            </a>
                        </td>
                    <?php endif ?>
                </tr>
            <?php endforeach ?>
        </tbody>
    </table>

    <button class="btn btn-primary" type="submit">
        <?= view('icons/save') ?>
        <?= I18N::translate('save') ?>
    </button>

    <a class="btn btn-secondary" href="<?= e(route(ControlPanel::class)) ?>">
        <?= view('icons/cancel') ?>
        <?= I18N::translate('cancel') ?>
    </a>
</form>

<?php View::push('javascript') ?>
<script>
  $('.wt-table-menu td.move').click(function () {
    let row = $(this).closest('tr');

    if ($(this).hasClass('up')) {
      row.prev().before(row);
    } else {
      row.next().after(row);
    }

    return false;
  });
</script>
<?php View::endpush() ?>

<?php View::push('styles') ?>
<style>
    .wt-table-menu tr:first-child .wt-icon-arrow-up {
        display: none;
    }

    .wt-table-menu tr:last-child .wt-icon-arrow-down {
        display: none;
    }
</style>
<?php View::endpush() ?>
